package com.gz.mylife.util;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;

import com.gz.mylife.R;


public class EmojiUtil {
	private static final String STATIC_EMOTION_REGEX = "<ef\\d{4}ef>";
	
	/**
	 * 对spanableString进行正则判断，如果符合要求，则以表情图片代替
	 * @param context
	 * @param spannableString
	 * @param patten
	 * @param start
	 * @param emojiSize 
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws NumberFormatException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
    public static void dealEmotion(Context context,SpannableString spannableString, Pattern patten, int start, int emojiSize) throws SecurityException, NoSuchFieldException, NumberFormatException, IllegalArgumentException, IllegalAccessException {
    	Matcher matcher = patten.matcher(spannableString);
        while (matcher.find()) {
            String key = matcher.group();
            String sourceStr = key;
            if (matcher.start() < start) {
                continue;
            }
            key = key.replaceAll("<", "");
            key = key.replaceAll(">", "");
            Field field = R.drawable.class.getDeclaredField(key);
			int resId = Integer.parseInt(field.get(null).toString());		//通过上面匹配得到的字符串来生成图片资源id
            if (resId != 0) {
            	Drawable d = context.getResources().getDrawable(resId);
            	d.setBounds(0, 0, emojiSize, emojiSize);
            	ImageSpan imageSpan = new ImageSpan(d, DynamicDrawableSpan.ALIGN_BASELINE);
            	
//                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
//                ImageSpan imageSpan = new ImageSpan(context, bitmap, DynamicDrawableSpan.ALIGN_BOTTOM);
                
                int end = matcher.start() + sourceStr.length();					//计算该图片名字的长度，也就是要替换的字符串的长度
                spannableString.setSpan(imageSpan, matcher.start(), end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);	//将该图片替换字符串中规定的位置中
                if (end < spannableString.length()) {						//如果整个字符串还未验证完，则继续。。
                    dealEmotion(context,spannableString,  patten, end, emojiSize);
                }
                break;
            }
        }
    }
    
    /**
     * 得到一个SpanableString的图文对象(如果包含图片正则表达式)，否则文字原样返回
     * @param context
     * @param emojiStr
     * @return
     */
    public static SpannableString getEmotionString(Context context,String emojiStr, int emojiSize){
    	if(TextUtils.isEmpty(emojiStr)){
    		emojiStr = "";
    	}
    	SpannableString spannableString = new SpannableString(emojiStr);
        Pattern patten = Pattern.compile(STATIC_EMOTION_REGEX, Pattern.CASE_INSENSITIVE);		
        try {
            dealEmotion(context,spannableString, patten, 0, emojiSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return spannableString;
    }
	
}